package view;



import dao.UserDAO;
import model.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 注册界面类，提供用户注册功能
 */
public class RegisterFrame extends JFrame {
    private JTextField usernameField;    // 用户名输入框
    private JPasswordField passwordField; // 密码输入框
    private JPasswordField confirmPasswordField; // 确认密码输入框
    private JTextField emailField;       // 邮箱输入框
    private JTextField phoneField;       // 电话输入框
    private JButton registerButton;      // 注册按钮
    private JButton backButton;          // 返回按钮
    private UserDAO userDAO;             // 用户数据访问对象

    /**
     * 构造方法，初始化注册界面
     */
    public RegisterFrame() {
        userDAO = new UserDAO();

        // 设置窗口标题和大小
        setTitle("在线购物系统 - 注册");
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // 创建面板并设置布局
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(6, 2, 10, 10));
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 创建用户名标签和输入框
        JLabel usernameLabel = new JLabel("用户名:");
        usernameField = new JTextField();

        // 创建密码标签和输入框
        JLabel passwordLabel = new JLabel("密码:");
        passwordField = new JPasswordField();

        // 创建确认密码标签和输入框
        JLabel confirmPasswordLabel = new JLabel("确认密码:");
        confirmPasswordField = new JPasswordField();

        // 创建邮箱标签和输入框
        JLabel emailLabel = new JLabel("邮箱:");
        emailField = new JTextField();

        // 创建电话标签和输入框
        JLabel phoneLabel = new JLabel("电话:");
        phoneField = new JTextField();

        // 创建注册按钮并添加事件监听器
        registerButton = new JButton("注册");
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText().trim();
                String password = new String(passwordField.getPassword());
                String confirmPassword = new String(confirmPasswordField.getPassword());
                String email = emailField.getText().trim();
                String phone = phoneField.getText().trim();

                // 验证输入
                if (username.isEmpty() || password.isEmpty() || confirmPassword.isEmpty() || email.isEmpty() || phone.isEmpty()) {
                    JOptionPane.showMessageDialog(RegisterFrame.this, "所有字段不能为空！", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                if (!password.equals(confirmPassword)) {
                    JOptionPane.showMessageDialog(RegisterFrame.this, "两次输入的密码不一致！", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                // 添加用户
                User user = new User();
                user.setUsername(username);
                user.setPassword(password);
                user.setEmail(email);
                user.setPhone(phone);

                userDAO.addUser(user);

                // 注册成功提示
                JOptionPane.showMessageDialog(RegisterFrame.this, "注册成功！请登录。", "成功", JOptionPane.INFORMATION_MESSAGE);

                // 关闭注册界面，返回登录界面
                dispose();
                new LoginFrame().setVisible(true);
            }
        });

        // 创建返回按钮并添加事件监听器
        backButton = new JButton("返回");
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 关闭注册界面，返回登录界面
                dispose();
                new LoginFrame().setVisible(true);
            }
        });

        // 将组件添加到面板
        panel.add(usernameLabel);
        panel.add(usernameField);
        panel.add(passwordLabel);
        panel.add(passwordField);
        panel.add(confirmPasswordLabel);
        panel.add(confirmPasswordField);
        panel.add(emailLabel);
        panel.add(emailField);
        panel.add(phoneLabel);
        panel.add(phoneField);
        panel.add(registerButton);
        panel.add(backButton);

        // 将面板添加到窗口
        add(panel);
    }
}